package com.ssh.multithread;

import java.util.LinkedList;
import java.util.Queue;

public class ThreadPool {
	
	private Queue<WorkThread> queue = new LinkedList<WorkThread>();

	private static final int _thread_count = 3;

	private void init() {
		System.out.println(this.toString());
		for (int i = 0; i < _thread_count; i++) {
			WorkThread wt = new WorkThread(this, i);
			queue.add(wt);
			wt.start();
		}
	}

	public void addTask(String task) {

		synchronized (queue) {
			// take
			WorkThread wt = queue.poll();
			// add a task
			wt.insertTask(task);
			// revoke
			queue.add(wt);
		}
	}
}
